package com.example.gamelogic;

import java.util.HashSet;
import java.util.Set;

public class Tournament {


	private String name;
	private Set<Team> team;
	private String type;
	private Set<GameDay> game;
	private StringBuilder content;

	public Tournament(String n, String t){
		this.name = n;
		this.type = t;
		team = new HashSet<Team>();
		content = new StringBuilder();
		createTable();
	}
	
	
	/**
	 * creates a Table with the Lig position
	 */
	public void createTable() {
		String position = "0";
		for(Team t: team){
			content.append(position+" "+t.getName()+" "+t.getGames_played()+" "+t.getGames_win()+" "
					+t.getGames_draw()+" "+t.getGames_lose()+" "+(t.getGoals()-t.getConcending_goal())
					+" "+t.getPoints());
		}
	}

	public void addTeams(Team t){
		this.team.add(t);
	}

	public boolean remove(Team t){
		if(team.contains(t)){
			team.remove(t);
			return true;
		}else
			return false;
	}
	
	public Team findTeam(int i){
		for(Team t : team){
			if(t.getTeamnumber() == i)
				return t;
		}
		return null;
	}

	public void setGamePlan(){
		int n=team.size();
		game = new HashSet<GameDay>();
		GameDay day;

		for(int i=1; i<=(n-1); i++){
			day = new GameDay(i);
			day.addMatch(new Match(findTeam(i),findTeam(n)));

			for (int k=1; k<=((n/2)-1); k++){

				int t1 = (((i+k)%(n-1))+(n-1))%(n-1);
				int t2 = (((i-k)%(n-1))+(n-1))%(n-1);
				if(t1==0){t1=(n-1);}
				if(t2==0){t2=(n-1);}
				
				day.addMatch(new Match(findTeam(t1),findTeam(t2)));
			}
			
			this.game.add(day);
		}
		
		int z=0;
		for(GameDay g: game){
			z++;
			System.out.println(z);
			for(Match m: g.getGame()){
				System.out.println(m.getHome().getName()+" vs. "+m.getAway().getName());
			}
			
		}
	}
	
	public void startTournament(){
		for(GameDay gameday: game){
			for(Match match: gameday.getGame()){
				match.play();
			}
		}
	}


	public static void main(String[] args) {
		Player p1 = new Player("Messi",Position.Forward,98);
		Player p2 = new Player("Ronaldo",Position.Forward,99);
		Player p3 = new Player("Scholes",Position.Middlefielder,78);
		Player p4 = new Player("Pogba",Position.Middlefielder,83);
		Player p5 = new Player("Boateng",Position.Defender,84);
		Player p6 = new Player("Czech",Position.Goalkeeper,85);

		Team t1 = new Team("Team 1");
		Team t2 = new Team("Team 2");
		Team t3 = new Team("Team 3");
		Team t4 = new Team("Team 4");

		t1.addPlayer(p1);
		t1.addPlayer(p2);
		t1.addPlayer(p3); 

		t2.addPlayer(p4);
		t2.addPlayer(p5);
		t2.addPlayer(p6);

		Tournament t = new Tournament("TestTurnier","Liga");
		t.addTeams(t1);
		t.addTeams(t2);
		t.addTeams(t3);
		t.addTeams(t4);
		t.setGamePlan();

		Match test = new Match(t2,t1);
		test.play();
		System.out.println("Gewonnen hat: Team"+test.result);

		System.out.println("Teamnumber: "+t4.getTeamnumber());

		/*
		for(Player p : t1.getPlayer()){
			System.out.println("Spieler: "+p.getName()+" gehört zu Team "+ t1.getName());
		}
		System.out.println("Der TeamWert beläuft sich auf "+t1.calculate_team_strong());*/

	}

}
